import { getToken, getPageString } from '@inbiz/utils';
import { routerPrefix } from 'InbizConfig';
import { getProcessInfo } from './service';

export interface TabObject {
  [x: string]: string;
  id: string;
  title: string;
  taskType: string;
  link: string;
}

const token = getToken();
export const pageRouter = getPageString();
export const currentPath = (route: string) => {
  return `${routerPrefix}${pageRouter}/${route}`;
};

export interface SearchCondition {
  processGroupName: string;
  processDefId: string;
  date: string;
  status: string;
  processCategoryName?: string | undefined;
  processName?: string | undefined;
  statusName: string | undefined;
  dateName: string | undefined;
}
export function getFormData(
  userInfo: any,
  pageIndex: number,
  name?: string,
  searchCondition?: SearchCondition,
) {
  const params = {
    userId: '',
    starter: '',
    pageIndex: 1,
    queryParameter: '',
    processGroupName: '',
    processDefinitionId: '',
    processDefId: '',
    date: '',
    status: '',
  };

  params.userId = userInfo?.ID;
  params.starter = userInfo?.ID;
  params.pageIndex = pageIndex;
  if (name) {
    params.queryParameter = name ?? '';
  }
  if (searchCondition) {
    params.processGroupName = searchCondition.processGroupName ?? '';
    params.processDefinitionId = searchCondition.processDefId ?? '';
    params.processDefId = searchCondition.processDefId ?? '';
    params.date = searchCondition.date ?? '';
    params.status = searchCondition.status ?? '';
  }
  return params;
}

export const jumpTo = (item: any, history: any) => {
  getProcessInfo({
    taskIds: item?.taskId,
    processInstanceId: item?.processInstanceId,
    inItiateApplication: false,
    taskState: item?.taskState,
    siteType: 3,
  }).then((res: any) => {
    if (res.details && res.details.length > 0) {
      history.push(`${routerPrefix}${getPageString()}/log?type=mobile&instanceId=${item?.processInstanceId}&processId=${res.details[0].processDefinitionId ?? res.details[0].processId}`)
    }
  });
};

export const toggleSelect = (bool: boolean, list: { task_id: string }[], callback: Function) => {
  const arr = list.map((item: { task_id: string }) => ({
    ...item,
    checked: bool,
  }));
  callback(arr);
};

export const statusList = [
  {
    id: '',
    name: 'processCenter.all',
  },
  {
    id: '10',
    name: 'processCenter.beenreturnsBack',
  },
  {
    id: '0',
    name: 'processCenter.approvaling',
  },
];
export const myapplystatusList = [
  {
    id: '',
    name: 'processCenter.all',
  },
  {
    id: '1',
    name: 'processCenter.complete',
  },
  {
    id: '0',
    name: 'processCenter.approvaling',
  },
];
export const dateList = [
  {
    id: '',
    name: 'processCenter.all',
  },
  {
    id: 'days',
    name: 'processCenter.days',
  },
  {
    id: 'week',
    name: 'processCenter.week',
  },
  {
    id: 'month',
    name: 'processCenter.month',
  },
];
